package com.dingding.configuration;

import com.dingding.aop.JWTInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpMethod;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class IntercepptorConfig implements WebMvcConfigurer {

    @Autowired
    private JWTInterceptor jwtInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(jwtInterceptor)
                .addPathPatterns("/**")
                .excludePathPatterns(
                        "/favicon.ico",
                        "/error",
                        "/user/login",
                        "/user/captch",
                        // swagger 文档配置
                        "/doc.html",
                        "/swagger-resources/**",
                        "/webjars/**",
                        "/*/api-docs"
                );
    }

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
             .allowCredentials(true)
            // 允许哪个请求来源进行跨域,  springboot升级成2.4.0以上时对AllowedOrigin设置发生了改变，不能有”*“
            // .allowedOrigins("*")
            .allowedOriginPatterns("*")
            .allowedMethods(new String[]{HttpMethod.GET.name(), HttpMethod.POST.name(), HttpMethod.OPTIONS.name(), HttpMethod.DELETE.name()})
            .allowedHeaders("*");
    }
}
